package com.smalljj.amq;

import java.math.BigDecimal;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smalljj.bean.Position;
import com.smalljj.bean.User;
import com.smalljj.service.PositionService;
import com.smalljj.service.UserService;
import com.smalljj.util.Constants;
import com.smalljj.util.SmalljjUtil;

@Service
public class GetMessage implements MessageListener {

	@Autowired
    private PositionService positionService;

	@Autowired
    private UserService userService;
	
	final static Logger logger = Logger.getLogger( GetMessage.class.getName());
	
	public void onMessage(Message msg) {
		//2345/20121220/34/34/2343423423.2323442/34234234234.234234234234/44
		//MjM0NS8yMDEyMTIyMC8zNC8zNC8yMzQzNDIzNDIzLjIzMjM0NDIvMzQyMzQyMzQyMzQuMjM0MjM0MjM0MjM0LzQ0
		Position position = null;
		User user = null;
		TextMessage message = (TextMessage) msg;
		try {
			String srcPosition = SmalljjUtil.decodeBase64(message.getText());
			//System.out.println("收到的消息:" + srcPosition);
			String[] para = srcPosition.split("/");
			position = new Position();
			position.setUserId(new BigDecimal(para[0]));//用户id
			position.setHour(new BigDecimal(para[2]));//时
			position.setMinute(new BigDecimal(para[3]));//分
			position.setLongitude(para[4]);//经
			position.setLatitude(para[5]);//纬
			position.setStaytime(new BigDecimal(para[6]));//时长
			positionService.insert(para[1],position);
			
			user = new User();
			user.setId(new BigDecimal(para[0]));
			user.setLastActiveDate(new BigDecimal(para[1]));
			user.setLastActiveTime(para[2]+":"+para[3]);
			user.setNowlon(para[4]);
			user.setNowlat(para[5]);
			userService.updateUser(user);
			
		} catch (Exception e) {
			logger.error("无效坐标串:"+e);
			if(Constants.ISDEBUG){
				e.printStackTrace();
			}
		}
	}
}
